package com.wencai.util;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.FileReader;
import java.io.IOException;

public class JSExecutor {

    //    private static String jsPath = "D:\\Code\\Stock\\backup\\Java\\Wencai\\src\\main\\resources\\hexin.js";
    private static String jsPath = "D:\\Code\\Stock\\backup\\Java\\Wencai\\env\\Lib\\site-packages\\pywencai\\hexin-v.js";

    public static void main(String[] args) {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("nashorn");

        try {
            // 执行简单脚本
            engine.eval(new FileReader(jsPath));

            Invocable invocable = (Invocable) engine;
            Object result = invocable.invokeFunction("v", 4, 5);
            System.out.println("Result: " + result); // 输出 8

        } catch (ScriptException e) {
            e.printStackTrace();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
    }
}
